package net.sf.jOpinionPoll.entity;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;

@Entity
@DiscriminatorValue("AL")
@NamedQueries({
		@NamedQuery(name = "QuestaoAlternativa.fetch.findById", query = "SELECT q FROM QuestaoAlternativa q WHERE q.id = :id"),
        @NamedQuery(name = "QuestaoAlternativa.findAll", query = "SELECT q FROM QuestaoAlternativa q"),
        @NamedQuery(name = "QuestaoAlternativa.count", query = "SELECT COUNT(q) FROM QuestaoAlternativa q")
         })
public class QuestaoAlternativa extends Questao {
	private static final long serialVersionUID = 1L;
	
	private Set<Alternativa> alternativas = new HashSet<Alternativa>();

	public QuestaoAlternativa(){
		super(null, null);
	}
	
    public QuestaoAlternativa(String descricao) {
    	super(descricao, null);
    }
    
    public QuestaoAlternativa(String descricao, Date data) {
    	super(descricao, data);
    }
	
	@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
	@JoinTable(name = "questao_alternativa",
			joinColumns=@JoinColumn(name = "questao_id", referencedColumnName =  "id"),
			inverseJoinColumns=@JoinColumn(name = "alternativa_id", referencedColumnName = "id"))
	public Set<Alternativa> getAlternativas() {
		return alternativas;
	}

	public void setAlternativas(Set<Alternativa> alternativas) {
		this.alternativas = alternativas;
	}
	
    @Override
    public int hashCode() {
        return getId();
    }
    
    @Override
    public boolean equals(Object o) {
        if (o == null || !(o instanceof QuestaoAlternativa) || !o.getClass().getName().equals(this.getClass().getName()))
        	return false;
        
        if (getId() != 0)
	        return (getId() == ((QuestaoAlternativa)o).getId());
        
        return this == o;
    }
    
    @Override
    public String toString() {
        return this.getClass().getName() + "[pk=" + getId() + "]";
    }
}